
/*
    File:       RTCPAckPacket.cpp

    Contains:   RTCPAckPacket de-packetizing class

    
*/


#include "RTCPAckPacket.h"
#include "RTCPPacket.h"
#include "MyAssert.h"
#include "OS.h"
//#include "OSMemory.h"
#include "OSArrayObjectDeleter.h"
#include <stdio.h>


Bool RTCPAckPacket::ParseAckPacket(UInt8* inPacketBuffer, UInt32 inPacketLen)
{
    fRTCPAckBuffer = inPacketBuffer;

    //
    // Check whether this is an ack packet or not.
    if ((inPacketLen < kAckMaskOffset) || (!this->IsAckPacketType()))
        return false;
    
    Assert(inPacketLen == (UInt32)((this->GetPacketLength() * 4)) + RTCPPacket::kRTCPHeaderSizeInBytes);
    fAckMaskSize = inPacketLen - kAckMaskOffset;
    return true;
}

Bool RTCPAckPacket::IsAckPacketType()
{
    // While we are moving to a new type, check for both
    UInt32 theAppType = ntohl(*(UInt32*)&fRTCPAckBuffer[kAppPacketTypeOffset]);
    
//  if ( theAppType == kOldAckPacketType ) qtss_printf("ack\n"); 
//  if ( theAppType == kAckPacketType ) qtss_printf("qtack\n");
                                                        
    return this->IsAckType(theAppType);
}

void   RTCPAckPacket::Dump()
{
    UInt16 theSeqNum = this->GetAckSeqNum();
    UInt16 thePacketLen = this->GetPacketLength();
    UInt32 theAckMaskSizeInBits = this->GetAckMaskSizeInBits();
    
    char name[5];
    name[4] = 0;
    
    ::memcpy(name, &fRTCPAckBuffer[kAppPacketTypeOffset],4);
    UInt16 numBufferBytes = (UInt16) ( (7 * theAckMaskSizeInBits) + 1 );
    char *maskBytesBuffer = new char[numBufferBytes];
    OSCharArrayDeleter deleter(maskBytesBuffer);
    maskBytesBuffer[0] = 0;
    maskBytesBuffer[numBufferBytes -1] = 0;
    for (UInt32 maskCount = 0; maskCount < theAckMaskSizeInBits; maskCount++)
    {
        if (this->IsNthBitEnabled(maskCount))
        {
            sprintf(&maskBytesBuffer[::strlen(maskBytesBuffer)],"%lu, ", theSeqNum + 1 + maskCount);
        }
    }
    Assert(::strlen(maskBytesBuffer) < numBufferBytes);
    printf(" H_name=%s H_seq=%u H_len=%u mask_size=%lu seq_nums_bit_set=%s\n",
                                    name, theSeqNum,thePacketLen,theAckMaskSizeInBits, maskBytesBuffer);

}

